﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Delonix_Regia_HMS.Classes;

namespace Delonix_Regia_HMS
{
    public partial class CreateStaff : Form
    {
        public CreateStaff()
        {
            InitializeComponent();
            InitializeComboBox();
        }
        private void btnCSCreateStaff_Click(object sender, EventArgs e)
        {
            bool create = false;
            if (string.IsNullOrEmpty(tbxSCStaffName.Text) || string.IsNullOrEmpty(tbxSCHomeAddress.Text) || string.IsNullOrEmpty(tbxSCPhoneNumber.Text))
            {
                MessageBox.Show("Missing required data to create staff");
            }
            else if (string.IsNullOrEmpty(tbxSCACNo1.Text) || string.IsNullOrEmpty(tbxSCACNo2.Text) || string.IsNullOrEmpty(tbxSCACNo3.Text))
            {
                MessageBox.Show("Your Missing Bank Account Number");
            }
            else
            {
                Staff s = new Staff();
                s.StaffName = tbxSCStaffName.Text;
                s.StaffDOB = Convert.ToDateTime(cbBoxCSYear.SelectedItem.ToString() + "/" + cbBoxCSMonth.SelectedItem.ToString() + "/" + cbBoxCSDate.SelectedItem.ToString());
                s.StaffBankAccountNumber = tbxSCACNo1.Text + "-" + tbxSCACNo2.Text + "-" + tbxSCACNo3.Text;
                s.StaffHomeAddress = tbxSCHomeAddress.Text;
                s.StaffHomeNumber = tbxSCPhoneNumber.Text;
                s.StaffType = "Pending";
                DBManager.insertStaff(s);
                create = true;
            }
            if (create == true)
            {
                
                MessageBox.Show("New Staff has been created");
                ClearTextBoxes(this.Controls);
            }
        }

        public void InitializeComboBox()
        {
            int date = 1;
            while (date <= 31)
            {
                cbBoxCSDate.Items.Add(date);
                date++;
            }

            int month = 1;
            while (month <= 12)
            {
                cbBoxCSMonth.Items.Add(month);
                month++;
            }

            int year = 1940;
            while (year <= Convert.ToInt32(DateTime.Now.Year.ToString()))
            {
                cbBoxCSYear.Items.Add(year);
                year++;
            }

        }
        #region Utilities
        // Clear all textboxes in control
        private void ClearTextBoxes(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                TextBox tb = ctrl as TextBox;
                if (tb != null)
                {
                    tb.Text = "";
                }

                else
                {
                    ClearTextBoxes(ctrl.Controls);
                }
            }
        }

        // This method only allows numeric to be press.
        private void restrictCharacter_KeyPress(object sender, KeyPressEventArgs e)
        {
            const char Delete = (char)8;
            e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
        }

        // Move to the next textbox
        public void autoExit(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
            if (tbxSCACNo1.TextLength == 3)
            {
                tbxSCACNo2.Focus();
            }
            if (tbxSCACNo2.TextLength == 5)
            {
                tbxSCACNo3.Focus();
            }
            
        }
        #endregion
    }
}
